import { ACCESS_TOKEN, USER_ID, NICK_NAME, STATUS } from '@/store/mutation-types'
import storage from '@/utils/storage'
import * as LoginApi from '@/api/login'
import * as UserApi from '@/api/user'

// 登陆成功后执行
const loginSuccess = (commit, { token }) => {
  // 过期时间30天
  const expiryTime = 30 * 86400
  // 保存tokne和userId到缓存
  storage.set(ACCESS_TOKEN, token, expiryTime)
  // 记录到store全局变量
  commit('SET_TOKEN', token)
}

// 登陆成功后执行
const userIdSuccess = (commit, { id, nick_name, status }) => {
	console.log('用户信息',status)
  // 过期时间30天
  const expiryTime = 30 * 86400
  // 保存tokne和userId到缓存
  storage.set(USER_ID, id, expiryTime)
  storage.set(NICK_NAME, nick_name, expiryTime)
  storage.set(STATUS, status, expiryTime)
  // 记录到store全局变量
  commit('SET_USER_ID', id)
  commit('SET_NICK_NAME', nick_name)
  commit('SET_STATUS', status)
}

const user = {
  state: {
    // 用户认证token
    token: '',
    // 用户ID
    userId: null,
	// 用户昵称
	nick_name: '',
	// 用户状态
	status: ''
  },

  mutations: {
    SET_TOKEN: (state, value) => {
      state.token = value
    },
    SET_USER_ID: (state, value) => {
      state.userId = value
    },
	SET_NICK_NAME: (state, value) => {
		state.nick_name = value
	},
	SET_STATUS: (state, value) => {
		state.status = value
	},
  },

  actions: {

    // 用户登录 (普通登录: 输入手机号和密码)
    Login({ commit }, data) {
      return new Promise((resolve, reject) => {
        LoginApi.login(data)
          .then(response => {
            const result = response.data
            loginSuccess(commit, result)
            resolve(response)
          })
          .catch(reject)
      })
    },

    // 微信小程序一键授权登录 (获取用户基本信息)
    LoginMpWx({ commit }, data) {
      return new Promise((resolve, reject) => {
        LoginApi.mpLogin({ code: data.code }, { isPrompt: false })
          .then(response => {
            const result = response.data
            loginSuccess(commit, result)
            resolve(response)
          })
          .catch(reject)
      })
    },

    // 微信小程序一键授权登录 (授权手机号)
    LoginMpWxMobile({ commit }, data) {
      return new Promise((resolve, reject) => {
        LoginApi.loginMpWxMobile({ form: data }, { isPrompt: false })
          .then(response => {
            const result = response.data
            loginSuccess(commit, result)
            resolve(response)
          })
          .catch(reject)
      })
    },
    // 用户登录 (普通登录: 输入手机号和验证码)
	LoginCodeWay({ commit }, data) {
      return new Promise((resolve, reject) => {
        LoginApi.loginCode(data)
          .then(response => {
            const result = response.data
            loginSuccess(commit, result)
            resolve(response)
          })
          .catch(reject)
      })
    },
	// 用户注册 (普通登录: 输入手机号和验证码)
	registerCodeWay({ commit }, data) {
      return new Promise((resolve, reject) => {
        LoginApi.registerCode(data)
          .then(response => {
            const result = response.data
            loginSuccess(commit, result)
            resolve(response)
          })
          .catch(reject)
      })
    },
	// 用户微信手机号登录
	mpRegisteWay({ commit }, data) {
      return new Promise((resolve, reject) => {
        LoginApi.mpRegiste(data)
          .then(response => {
            const result = response.data
            loginSuccess(commit, result)
            resolve(response)
          })
          .catch(reject)
      })
    },
    // 获取当前用户信息
    GetUserInfo({ commit }) {
        const app = this
        return new Promise((resolve, reject) => {
          UserApi.info({})
            .then(result => {
              const userInfo = result.data
			  userIdSuccess(commit, userInfo)
              resolve(userInfo)
            })
            .catch(reject)
        })
    },
    // 退出登录
    Logout({ commit }, data) {
      const store = this
      return new Promise((resolve, reject) => {
		  console.log(store.getters.userId,'store.getters.userId')
        if (store.getters.userId > 0) {
          // 删除缓存中的tokne和userId
          storage.remove(USER_ID)
          storage.remove(ACCESS_TOKEN)
		  storage.remove(NICK_NAME)
          // 记录到store全局变量
          commit('SET_TOKEN', '')
          commit('SET_USER_ID', null)
		  commit('SET_NICK_NAME', '')
          resolve()
        }
      })
    }

  }
}

export default user
